﻿//Copyright (c) 2009, Offbeat Guides
//All rights reserved.
//
//Redistribution and use in source and binary forms, with or without modification, are 
//permitted provided that the following conditions are met:
//
//* Redistributions of source code must retain the above copyright notice, this list of 
//conditions and the following disclaimer.
//
//* Redistributions in binary form must reproduce the above copyright notice, this list 
//of conditions and the following disclaimer in the documentation and/or other materials 
//provided with the distribution.
//
//* Neither the name of Offbeat Guides nor the names of its contributors may be used to 
//endorse or promote products derived from this software without specific prior written 
//permission.
//
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
//EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
//OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
//SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
//SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
//OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
//HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
//TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
//EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

using System.Data;
using System;
using Amazon.SimpleDB.Model;

namespace Amazon.SimpleDB
{
    public class DomainRow : IDataRecord
    {
        #region Properties
        private Item BaseItem { get; set; }
        public Domain Domain { get; private set; }
        #endregion

        #region Constructors and Finalizers
        internal DomainRow(Domain domain, Item item)
        {
            this.BaseItem = item;
            this.Domain = domain;
        }
        #endregion

        #region IDataRecord Properties
        public int FieldCount
        {
            get { return this.BaseItem.Attribute.Count + 1; }
        }

        public object this[string name]
        {
            get { return this.GetValue(this.GetOrdinal(name)); }
        }

        public object this[int i]
        {
            get { return this.GetValue(i); }
        }
        #endregion

        #region IDataRecord Methods
        public bool GetBoolean(int i)
        {
            if (i == 0)
                return Convert.ToBoolean(this.BaseItem.Name);
            return Convert.ToBoolean(this.BaseItem.Attribute[i - 1].Value);
        }

        public byte GetByte(int i)
        {
            if (i == 0)
                return Convert.ToByte(this.BaseItem.Name);
            return Convert.ToByte(this.BaseItem.Attribute[i - 1].Value);
        }

        public long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length)
        {
            throw new System.NotImplementedException();
        }

        public char GetChar(int i)
        {
            if (i == 0)
                return Convert.ToChar(this.BaseItem.Name);
            return Convert.ToChar(this.BaseItem.Attribute[i - 1].Value);
        }

        public long GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length)
        {
            throw new System.NotImplementedException();
        }

        public IDataReader GetData(int i)
        {
            throw new System.NotImplementedException();
        }

        public string GetDataTypeName(int i)
        {
            return typeof(String).FullName;
        }

        public System.DateTime GetDateTime(int i)
        {
            throw new System.NotImplementedException();
        }

        public decimal GetDecimal(int i)
        {
            if (i == 0)
                return Convert.ToDecimal(this.BaseItem.Name);
            return Convert.ToDecimal(this.BaseItem.Attribute[i - 1].Value);
        }

        public double GetDouble(int i)
        {
            if (i == 0)
                return Convert.ToDouble(this.BaseItem.Name);
            return Convert.ToDouble(this.BaseItem.Attribute[i - 1].Value);
        }

        public System.Type GetFieldType(int i)
        {
            return typeof(String);
        }

        public float GetFloat(int i)
        {
            if (i == 0)
                return Convert.ToSingle(this.BaseItem.Name);
            return Convert.ToSingle(this.BaseItem.Attribute[i - 1].Value);
        }

        public System.Guid GetGuid(int i)
        {
            throw new System.NotImplementedException();
        }

        public short GetInt16(int i)
        {
            if (i == 0)
                return Convert.ToInt16(this.BaseItem.Name);
            return Convert.ToInt16(this.BaseItem.Attribute[i - 1].Value);
        }

        public int GetInt32(int i)
        {
            if (i == 0)
                return Convert.ToInt32(this.BaseItem.Name);
            return Convert.ToInt32(this.BaseItem.Attribute[i - 1].Value);
        }

        public long GetInt64(int i)
        {
            if (i == 0)
                return Convert.ToInt64(this.BaseItem.Name);
            return Convert.ToInt64(this.BaseItem.Attribute[i - 1].Value);
        }

        public string GetName(int i)
        {
            if (i == 0)
                return "itemName";
            return this.BaseItem.Attribute[i].Name;
        }

        public int GetOrdinal(string name)
        {
            if (name.ToLower() == "itemname")
                return 0;
            for (int i = 0; i < this.BaseItem.Attribute.Count; i++)
            {
                if (name.ToLower().Equals(this.BaseItem.Attribute[i].Name.ToLower()))
                    return i + 1;
            }
            return -1;
        }

        public string GetString(int i)
        {
            if (i == 0)
                return this.BaseItem.Name;
            return this.BaseItem.Attribute[i - 1].Value;
        }

        public object GetValue(int i)
        {
            if (i == 0)
                return this.BaseItem.Name;
            return this.BaseItem.Attribute[i - 1].Value;
        }

        public int GetValues(object[] values)
        {
            throw new System.NotImplementedException();
        }

        public bool IsDBNull(int i)
        {
            return false;
        }
        #endregion
    }
}
